// $Id$
package cz.pd.projectbuilder.main.gui.panel;

import com.jidesoft.swing.CheckBoxTree;
import com.jidesoft.swing.SearchableUtils;
import cz.pd.projectbuilder.beans.load.ProjectBean;
import cz.pd.projectbuilder.beans.load.ProjectGroupBean;
import cz.pd.projectbuilder.main.listener.InfoListener;
import cz.pd.projectbuilder.main.listener.ProjektyListener;
import cz.pd.projectbuilder.main.util.BeanUtils;
import cz.pd.projectbuilder.main.util.Info;
import cz.pd.projectbuilder.main.util.ListenersUtils;
import cz.pd.projectbuilder.main.util.ProjectUtils;
import java.awt.Color;
import java.awt.Component;
import java.beans.Beans;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

/**
 *
 * @author diblik
 * @version $Revision$
 */
public class ProjektyPanel extends JPanel {

  /** Creates new form ProjektyFrame */
  public ProjektyPanel() {
    initComponents();
    if (!Beans.isDesignTime()) {
      ListenersUtils.addListener(new ProjektyListener() {

        @Override
        public void afterLoadProjectsData(List<ProjectGroupBean> projectGroupBeans) {
          generateTabbedPaneProjekty(projectGroupBeans);
        }

        @Override
        public void afterLoadSuitsData(List<ProjectGroupBean> projectGroupBeans) {
        }
      });
    }
  }

  private void generateTabbedPaneProjekty(List<ProjectGroupBean> projectGroupBeans) {
    for (ProjectGroupBean projectGroupBean : projectGroupBeans) {
      DefaultMutableTreeNode root = new DefaultMutableTreeNode("All");

      for (ProjectBean projectBean : projectGroupBean.getProjectBeans()) {
        addToTreeModel(root, projectBean);
      }

      DefaultTreeModel treeModel = new DefaultTreeModel(root);
      createZalozku(projectGroupBean.getGroupName(), treeModel);
    }
  }

  private void createZalozku(String name, DefaultTreeModel treeModel) {
    JScrollPane jScrollPane = new javax.swing.JScrollPane();
    CheckBoxTree tree = new CheckBoxTree(treeModel);
    tree.setRootVisible(false);
    tree.setShowsRootHandles(true);
    SearchableUtils.installSearchable(tree);
    jScrollPane.setViewportView(tree);
    jScrollPane.setBorder(null);
    tabbedPaneProjekty.addTab(name, jScrollPane);

//     DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer() {
//
//      @Override
//      public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
//        Component rendererComponent = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
//        TreePath path = tree.getPathForRow(row);
////        ((CheckBoxTree) tree).getCheckBoxTreeSelectionModel().setSelectionPath(path);
//        if (((CheckBoxTree) tree).getCheckBoxTreeSelectionModel().isPathSelected(path, true)) {
//          rendererComponent.setForeground(Color.gray);
//        }
//        return rendererComponent;
//      }
//    };
//
//    tree.setCellRenderer(renderer);

    tree.getCheckBoxTreeSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {

      @Override
      public void valueChanged(TreeSelectionEvent e) {

        TreePath[] paths = e.getPaths();
        ListenersUtils.fire(InfoListener.class).showInfo("----------------------");
        for (TreePath path : paths) {
          Info.addInfo((e.isAddedPath(path) ? "Added - " : "Removed - ") + path + ", count " + path.getPathCount() + ", lastCmp " + path.getLastPathComponent() + ", parent " + path.getParentPath());
          if (e.isAddedPath(path)) {
            ProjectUtils.DAVKA.addDavkaItem((ProjectBean)((DefaultMutableTreeNode) path.getLastPathComponent()).getUserObject());
          } else {
            ProjectUtils.DAVKA.removeDavkaItem((ProjectBean)((DefaultMutableTreeNode) path.getLastPathComponent()).getUserObject());
          }
        }
      }
    });
  }

  private void addToTreeModel(DefaultMutableTreeNode root, ProjectBean projectBean) {
    DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode(projectBean);
    root.add(treeNode);
    if (projectBean.getSubprojekty()) {
      ProjectBean newSubProjectBeanRcp = BeanUtils.generateNewSubProjectBean(projectBean, BeanUtils.SubProjectType.RCP);
      ProjectBean newSubProjectBeanService = BeanUtils.generateNewSubProjectBean(projectBean, BeanUtils.SubProjectType.SERVICE);
      ProjectBean newSubProjectBeanServiceImpl = BeanUtils.generateNewSubProjectBean(projectBean, BeanUtils.SubProjectType.SERVICEIMPL);
      treeNode.add(new DefaultMutableTreeNode(newSubProjectBeanRcp));
      treeNode.add(new DefaultMutableTreeNode(newSubProjectBeanService));
      treeNode.add(new DefaultMutableTreeNode(newSubProjectBeanServiceImpl));
    }
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {
    java.awt.GridBagConstraints gridBagConstraints;

    tabbedPaneProjekty = new javax.swing.JTabbedPane();

    setLayout(new java.awt.GridBagLayout());

    tabbedPaneProjekty.setName("tabbedPaneProjekty"); // NOI18N
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.weighty = 1.0;
    add(tabbedPaneProjekty, gridBagConstraints);
  }// </editor-fold>//GEN-END:initComponents
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JTabbedPane tabbedPaneProjekty;
  // End of variables declaration//GEN-END:variables
}
